<template>
    
    <div style="padding-top: 20px;">
        <div class="tabbar">
            <div v-for="v in tabflist" :class="{cur:query.cid==v.id}" @click="changeCid(v.id)">
{{ v.name }}
            </div>
        </div>
       <el-button @click='dialogVisible="true"'> 发帖</el-button>
       <div class="list">
        <div class="item " v-for="v in list" @click="goDetail(v.id)">
        <div class="left">
            <img :src="v.avatar" alt="">
        </div>
        <div class="right">
            <div>{{ v.title }}</div>
            <div>所属分类：{{ v.cname }}</div>
            <div>用户：{{ v.uname }} <span>{{ v.time }}</span>   <span v-if="v.status==1">待审核</span></div>
        </div>
       </div>
       </div>
    <el-dialog
  title="发帖"
  :visible="dialogVisible"
  width="400px"
  :before-close="handleClose">
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
  <el-form-item label="标题" prop="title">
    <el-input v-model="ruleForm.title" placeholder="请输入标题"></el-input>
  </el-form-item>
  <el-form-item label="分类" prop="title">
    <el-select v-model="ruleForm.cid" style="width: 100%;">
        <el-option v-for="v in fenleiList" :value="v.id" :label="v.name" :key="v.id"/>
    </el-select>
  </el-form-item>
<el-form-item prop="avatar"  label="封面图" >
    <el-upload
  class="avatar-uploader"
 action="/api/upload"
  :show-file-list="false"
  :on-success="handleAvatarSuccess"
  >
  <img v-if="ruleForm.avatar" :src="ruleForm.avatar" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>

</el-form-item>
<el-form-item prop="img"  label="图片" >
    <el-upload
  
 action="/api/upload"
  :show-file-list="false"
  :on-success="handleAvatarSuccess1"
  >
  <img v-if="ruleForm.img" :src="ruleForm.img" style="width: 100%;">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>

</el-form-item>
<el-form-item prop="furl"  label="附件" >
    <el-upload
  class="upload-demo"
  action="/api/upload"
  :limit="1" :on-success="handleAvatarSuccess2"
  :file-list="fileList">
  <el-button size="small" type="primary">点击上传</el-button>
</el-upload>

</el-form-item>
<el-form-item label="内容" prop="content">
    <el-input v-model="ruleForm.content" placeholder="请输入内容" type="textarea"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm">确 定</el-button>
  </span>
</el-dialog>
    </div>

</template>
<script>
    import api from "@/api/index"
    export default {
        data() {
            return {
                query:{
                    cid:null,
                    page:1,
                    size:99,
                    uid:this.$store.state.user.id,
                    status:2
                },
                loginType:1,
                dialogVisible: false,
                fenleiList:[],
                tabflist:[],
                list:[],
                ruleForm: {},
                rules: {
                    title: [{
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    }, ],
                    cid: [{
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    }, ],
                     avatar: [{
                         required: true,
                         message: '不能为空',
                         trigger: 'blur'
                     }, ],
                     img: [{
                         required: true,
                         message: '不能为空',
                         trigger: 'blur'
                     }, ],
                     furl: [{
                         required: true,
                         message: '不能为空',
                         trigger: 'blur'
                     }, ],
                    content: [{
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    }, ],
                }
            }
        },
mounted(){
    api.getfenleiList({page:1,size:99,type:2}).then(res=>{
                    this.fenleiList=[...res.data.list]
                    this.tabflist=[...res.data.list]
                    this.tabflist.unshift({
                        name:'全部',
                        id:null
                    })
                })
      this.getList()
},
        methods: {
            goDetail(id){
                this.$router.push('/tieziDetail?id='+id)
            },
            getList(){
                api.gettiezi(this.query).then(res=>{
                   this.list=res.data.list
                })
            },
            handleClose() {

this.dialogVisible=false
},
            changeCid(cid){
                this.query.cid=cid
                this.getList()
            },
            handleAvatarSuccess(e){
                this.$set( this.ruleForm,'avatar',e.data)
            },
            handleAvatarSuccess2(e){
                this.$set( this.ruleForm,'furl',e.data)
            },
            handleAvatarSuccess1(e){
                this.$set( this.ruleForm,'img',e.data)
            },
            submitForm(formName) {
                this.$refs.ruleForm.validate((valid) => {
                    if(valid){
                        let obj={
...this.ruleForm,uid:this.$store.state.user.id,uname:this.$store.state.user.name
                    }
                api.addtiezi(obj).then(res=>{
                    this.dialogVisible=false
                    this.$message({
                        message: `发布成功`,
                        type: 'success'
                    });
                    this.getList()
                })
                    }
                  
                });
            },

        },
    }
</script>

<style lang="scss" scoped>
    .list{
        padding: 20px;
        .item{
       cursor: pointer;
        margin-bottom: 20px;
        display: flex;
        .left{
            img{
                width: 120px;
                height: 120px;
            }
        }
        .right{
            padding: 10px;
            flex:1;
            border: 1px solid gray;
            border-left: none;
            >div:nth-child(1){
                font-size: 16px;
                font-weight: bold;
            }
            >div:nth-child(2){
                height: 50px;
                line-height: 50px;
            }
            span{
                color: gray;
            }
            &:hover{
                background-color: #ddd;
            }
        }
    }
    }
   
.tabbar{
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    >div{
        cursor: pointer;

        border: 1px solid #ddd;
        margin-right: 20px;
        padding: 6px 12px;
        &:hover{
            color: white;
            background-color: #409EFF;
        }
        &.cur{
            color: white;
            background-color: #409EFF;
        }
    }
}
 ::v-deep .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
    * {
        box-sizing: border-box;
    }
    
    .active {
        color: blue !important;
    }
    
    .header {
        width: 100%;
        height: 79px;
        border-bottom: 1px solid rgb(155, 155, 155);
        font-size: 18px;
        padding: 20px;
        color: white;
        background-color: cornflowerblue;
        display: flex;
        align-items: center;
        .center {
            >div {
                display: flex;
                justify-content: center;
            }
            flex-grow: 1;
        }
        .left {
            flex: 1;
            display: flex;
            >div:not(:nth-child(1)) {
                flex: 1;
                cursor: pointer;
                margin-right: 30px;
            }
        }
        .right {
            text-align: right;
            width: 300px;
        }
    }
</style>